<?php

class special_courses_grade_controller {
	
	private $params = NULL;
	
	function __construct($params) {
		$this->set_params($params);
		$this->init_access();
	}
	
	function __destruct() {
		
	}
	
	function set_params($params) {
		$this->params = $params;
	}
	
	private function init_access() {
		global $authentication;
		$access = ACCESS_GROUP_ADMIN | 
			ACCESS_GROUP_DIRECTOR_OF_STUDIES | 
			ACCESS_GROUP_ASSISTANT | 
			ACCESS_GROUP_EDUCATOR | 
			ACCESS_GROUP_SECRETARY_OF_THE_DEPARTMENT;
		if (! $authentication->have_access($access)) {
			$authentication->access_denied();
		}
	}
	
	/**
	 * Загружает стартовую форму для отчета по оценкам спецкурсов
	 */
	function get_page() {
		global $CFG;
		
		include_once($CFG->lib_dir . '/views/class.view.php');
		
		$view = new view(
			__DIR__ . '/views/show_form.php',
			array(
				'classes' => $this->get_classes()
			)
		);
		$view->render();
	}
	
	/**
	 * Получает классы, которые доступны пользователю
	 */
	private function get_classes() {
		global $CFG;
		include_once($CFG->app_dir . '/class/class.classes.php');
		
		global $authentication;
		if ($authentication->have_access(ACCESS_GROUP_ADMIN)) {
			$classes = new classes('all', true);
		} else if ($authentication->have_access(ACCESS_GROUP_DIRECTOR_OF_STUDIES)) {
			$classes = new classes('all', true);
		} else if ($authentication->have_access(ACCESS_GROUP_ASSISTANT)) {
			$user_data = $authentication->get_user_data();
			$classes = new classes($user_data['streem'], true);
		} else if ($authentication->have_access(ACCESS_GROUP_EDUCATOR)) {
			$user_data = $authentication->get_user_data();
			$classes = new classes($user_data['class'], true);
		} else if ($authentication->have_access(ACCESS_GROUP_SECRETARY_OF_THE_DEPARTMENT)) {
			$classes = new classes('all', true);
		} else {
			$authentication->access_denied();
		}
		return $classes->get_classes();
	}
	
	function show_classes() {
		global $CFG;
		
		include_once($CFG->lib_dir . '/views/class.view.php');
		
		$view = new view(
			__DIR__ . '/views/show_classes.php',
			array(
				'classes' => $this->get_classes()
			)
		);
		$view->render_partial();
	}
	
	function show_departments() {
		global $CFG;
		include_once($CFG->app_dir . '/department/class.departments.php');
		include_once($CFG->lib_dir . '/views/class.view.php');
		
		// TODO : добавить фильтр для прав пользователей
		$departments = new departments();
		
		$view = new view(
			__DIR__ . '/views/show_departments.php',
			array(
				'departments' => $departments->get_departments()
			)
		);
		$view->render_partial();
	}
	
	function show_additional_parameters() {
		if ($_POST['report_type'] == 'by_class') {
			$this->show_classes();
		} else if ($_POST['report_type'] == 'by_department') {
			$this->show_departments();
		}
	}
	
	/**
	 * Показывает отчет по оценкам спецкурсов
	 */
	function show_report() {
		global $CFG;
		include_once($CFG->app_dir . '/grade/class.grade_manager.php');
		include_once($CFG->lib_dir . '/views/class.view.php');
		
		$grade_manager = new grade_manager();
		$data = $grade_manager->get_special_courses_grades($_POST['report_type'], $_POST['param']);
		
		if ($_POST['report_type'] == 'by_class') {
			$view = new view(
				__DIR__ . '/views/show_report_by_class.php',
				array(
					'data' => $data
				)
			);
		} else {
			$view = new view(
				__DIR__ . '/views/show_report_by_department.php',
				array(
					'data' => $data
				)
			);
		}
		$view->render_partial();
	}
	
	function action_show() {
		if (empty($this->params)) {
			$this->get_page();
		} else if ($this->params[0] == 'additional_parameters') {
			$this->show_additional_parameters();
		} else if ($this->params[0] == 'report') {
			$this->show_report();
		}
	}
}

?>
